package com.woniuxy.session.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.session.entity.Goods;
import com.woniuxy.session.mapper.GoodsMapper;
import com.woniuxy.session.service.GoodsService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

/**
 * @Author: linKe
 * @Date: 2022/1/6 16:52
 * @FileName: GoodsServiceImpl
 * @version: 1.0
 * @Description: 商品服务实现类
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    RedissonClient redissonClient;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    GoodsMapper goodsMapper;

    private int totalNum;
    /**
     * 根据id购买商品
     *
     * @param id 商品id
     * @return 结果
     */
    @Override
    public Goods updateGoods(int id) {
        RLock myLock = redissonClient.getLock("myLock");
        myLock.lock();
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        String stock = (String) opsForValue.get(id + "stock");
        Goods goods = null;
        if (stock == null || stock == "0") {
            goods = goodsMapper.selectById(id);
            opsForValue.set(id + "stock", "" + goods.getStock());
            System.out.println("数据库更新redis数据");
        } else {
            int count = Integer.parseInt(stock);
            if (count == 0) {
                System.out.println(id + "商品已卖完");
                return null;
            }
            count--;
            opsForValue.set(id + "stock", count + "");

            //goodsMapper.updateById(new Goods(id,"",count));

            goodsMapper.update(new Goods(id, "黑格尔《小逻辑》", count), null);
            System.out.println(id + "当前剩余库存:" + count);
        }
        myLock.unlock();

        return goodsMapper.selectById(id);
    }

    @Override
    public void init(int num) {
        totalNum = num;
        goodsMapper.updateById(new Goods(1,"黑格尔《小逻辑》",num));
        redisTemplate.opsForValue().set("goodsNum1",num);
        redisTemplate.opsForSet().add("userId", 0);
    }


    @Override
    public void buy(int goodsId, int userId) {
        RLock lock = redissonClient.getLock("goodsLock");
        lock.lock();

        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        SetOperations<String, Object> opsForSet = redisTemplate.opsForSet();

        Object goodsNum = opsForValue.get("goodsNum" + goodsId);
        //判断用户超卖的变量
        Boolean member = opsForSet.isMember("userId", userId);

        if (goodsNum == null) {
            System.out.println("商品卖完了,没有操作数据库");
            // 如果还有商品且该用户还没下单则进行下单
        }else if ((Integer) goodsNum != 0 && !member) { 
            opsForValue.increment("goodsNum"+goodsId,-1);
            opsForSet.add("userId", userId);
            totalNum = (Integer) opsForValue.get("goodsNum" + goodsId);
            System.out.println("下单成功,剩下商品数量：" + opsForValue.get("goodsNum" + goodsId));
        } else if (member) {
            System.out.println("已下单，不可重复下单");
        } else if ((Integer)goodsNum == 0) {
            System.out.println("商品卖完了");
            redisTemplate.delete("goodsNum" + goodsId);
            goodsMapper.update(new Goods(1,"黑格尔《小逻辑》",goodsId),null);
            //goodsDao.updateNum(goodsId);
        }

        lock.unlock();
    }
}
